package com.easipass.framework.eirframeworkwebapp.controller;

import com.easipass.framework.eirframeworkjdbc.MessageLogService;
import com.easipass.framework.eirframeworkwebapp.QueryParams;
import com.google.gson.Gson;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class MessageTestController {

    @Autowired
    MessageLogService messageLogService;

    @RequestMapping("/message/add")
    public String addMessage(@RequestBody QueryParams queryParams) {
        Map<String, Object> param = queryParams.getParam();
        return messageLogService.save(MapUtils.getString(param, "name"),
                MapUtils.getString(param, "serviceName"),
                MapUtils.getString(param, "appName"),
                MapUtils.getString(param, "content")).toString();
    }

    @RequestMapping("/message/get")
    public String getMessage() {
        return new Gson().toJson(messageLogService.getMessageLogRepository().findAll());
    }

    @RequestMapping("/message/get/{name}")
    public String getMessageByName(@PathVariable("name") String name) {
        return new Gson().toJson(messageLogService.getMessageLogRepository().findByName(name));
    }
}
